#!/usr/bin/python
import requests
from bs4 import BeautifulSoup
import matplotlib.pyplot as plt
import re

heightMap = []
lowMap = []
dayMap = []


def getWeather(url):
    response = requests.get(url)
    # 确保请求成功
    if response.status_code == 200:
        # 使用BeautifulSoup解析网页内容
        soup = BeautifulSoup(response.text, "html.parser")
        div_list = soup.find_all("div", class_="list")
        global heightMap
        global dayMap
        for div in div_list:
            listLeft = div.find("div", class_="list_left")
            # print(listLeft.get_text())
            # 使用正则表达式找到所有的数字
            s = listLeft.get_text().strip()
            numbers = re.findall(r'\d+', s)
            num = ""
            for number in numbers:
                num += number
            dayMap.append(num)
            top = div.find("div", class_="top")
            bottom = div.find("div", class_="bottom")

            topCommon = top.find_all("div", class_="common")

            heightNum = topCommon[3].get_text().strip()

            bottomCommon = bottom.find_all("div", class_="common")

            lowNum = bottomCommon[3].get_text().strip()

            # heightNum.string = str(heightNum.string)
            heightNum = str(heightNum).replace("高", "").replace("低", "").replace("℃", "").replace("温", "")
            lowNum = str(lowNum).replace("高", "").replace("低", "").replace("℃", "").replace("温", "")

            lowMap.append(int(lowNum))
            heightMap.append(int(heightNum))


def doPic(x, y, z):
    # 创建图形
    plt.plot(x, y, c='red')
    plt.scatter(x, y, c='red')
    plt.plot(x, z, c='blue')
    plt.scatter(x, z, c='blue')
    plt.rcParams['font.sans-serif'] = ['SimHei']  # 用来正常显示中文标签
    plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号

    # 设置图形标题和坐标轴标签
    plt.title('雷州市气温走势图')
    plt.xlabel('时间(红色最高气温/蓝色最低气温)')
    plt.ylabel('温度(℃)')

    # 显示图形
    plt.show()


def doHotWeather(url):
    response = requests.get(url)
    # 确保请求成功
    if response.status_code == 200:
        # 使用BeautifulSoup解析网页内容
        soup = BeautifulSoup(response.text, "html.parser")
        divs = soup.find("div", class_="mapcoms")
        div_list = divs.find_all("div", class_="csli")
        list = []
        for div in div_list:
            spans = div.find_all("span")
            city = spans[0]
            city_url = city.find("a").attrs.get("href")
            weather_style = spans[1]
            temperature = spans[2]
            wind = spans[3]
            #print(city.get_text(), city_url, weather_style, temperature.get_text(), wind.get_text())
            maps = {"city": city.get_text(), "city_url": city_url, "weather_style": weather_style.get_text(),
                   "temperature": temperature.get_text(), "wind": wind.get_text()}
            list.append(maps)
        return list


if __name__ == '__main__':
    '''
    # list = doHotWeather("https://www.713772.com/")
    # print(list)
    '''

